package com.itheima.reverseIndex;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class ReverseIndexMapperTask extends Mapper<LongWritable, Text,Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String line = value.toString();
        if(line!=null && !"".equals(line)){
            String[] words = line.split(" ");
            //获取数据是哪一个文件的
            FileSplit fileSplit =(FileSplit) context.getInputSplit();
            String fileName = fileSplit.getPath().getName();

            for (String word : words) {
                context.write(new Text(word+"_"+fileName),new IntWritable(1));
            }
        }
    }
}
